import {Injectable} from "@nestjs/common"
import {userInfo} from "./interface/utils"


@Injectable()
export class utils {

    constructor(){}

    // 日期格式化

    dateConversion = (value) => {
        var d = new Date(value);
        var date =
            d.getFullYear() +
            "-" +
            (d.getMonth() + 1) +
            "-" +
            d.getDate() +
            " " +
            d.getHours() +
            ":" +
            d.getMinutes() +
            ":" +
            d.getSeconds();
        return date;
    }

    // 日期天数，小时，分钟格式化

    calculateDiffDate(startDate) {

        if (!startDate) return "";
        if (!(startDate instanceof Date)) {
          startDate = new Date(startDate);
        }
        let endDate = new Date()
    
        const startTime = startDate.getTime();
        const endTime = endDate.getTime();
        const total = (endTime - startTime) / 1000;
        const d = total/(365*24*60*60) as unknown as string
        // const year = parseInt(d)
        const a =total/(24 * 60 * 60) as unknown as string
        const day = parseInt(a); // 计算整数天数
        const afterDay = (total - day * 24 * 60 * 60) ; // 取得值算出天数后剩余的转秒数
        const b = afterDay / (60 * 60) as unknown as string
        const hour = parseInt(b); // 计算整数小时数
        const afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60; // 取得算出小时数后剩余的秒数
        const c =afterHour / 60 as unknown as string
        const min = parseInt(c); // 计算整数分
        const afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60; // 取得算出分后剩余的秒数
        
        const result = {
          day,
          hour,
          min,
          // year
        };
        return result;
      }

      //处理一下作品数据的格式
      worksFormData(data:userInfo[]){
        let newArr = []
        // console.log(data)
        data.forEach(item=>{
            item.works?.forEach(info=>{
              info.username = item.username
              info.createTime = this.dateConversion(info.createTime)
              newArr.push(info)
            })
        })

        return newArr
      }


      // 一窜随机数
      randomNo(){
        let date = new Date()
        let years = date.getFullYear()
        let months = date.getMonth()+1
        let order = Math.floor(Math.random()*1000+1)
        let employeeNo = 11355+""+years+""+months+""+order

        return employeeNo
      }

      //生成随机编号
       orderCode() {
        var orderCode='';
        for (var i = 0; i < 6; i++) //6位随机数，用以加在时间戳后面。
        {
          orderCode += Math.floor(Math.random() * 10);
        }
        orderCode = new Date().getTime() + orderCode;  //时间戳，用来生成订单号。
        // console.log(orderCode)
        return orderCode;
      }
      

}